import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_li_ke_jie/ui/auth/real_name_page.dart';
import 'package:flutter_li_ke_jie/ui/error/error_page.dart';
import 'package:flutter_li_ke_jie/ui/goods/goods_info_page.dart';
import 'package:flutter_li_ke_jie/ui/home/home_page.dart';
import 'package:flutter_li_ke_jie/ui/product/product_page.dart';
import 'package:flutter_li_ke_jie/ui/login/login_page.dart';
import 'package:flutter_li_ke_jie/ui/start/start_page.dart';
import 'package:flutter_li_ke_jie/utils/navigator_util.dart';

import '../entity/app_page.dart';
import '../init/default_app.dart';
import '../utils/nav_key.dart';

final String ERROR_PAGE = "/error";
final String LOGIN_PAGE = "/login";
final String START_PAGE = "/start";
final String HOME_PAGE = "/home";
final String REAL_NAME_PAGE = "/real_name_page";
final String PRODUCT_PAGE = "/product_page";
final String GOODS_INFO_PAGE = "/goods_info_page";


///路由
final routes={
  HOME_PAGE:AppPage((context)=>const HomePage()),//首页
  START_PAGE:AppPage((context)=>const StartPage()),//启动页
  REAL_NAME_PAGE:AppPage((context)=>const RealNamePage()),//实名认证
  LOGIN_PAGE:AppPage((context)=>const LoginPage(),needLogin: false),//登录页面
  ERROR_PAGE:AppPage((context)=>ErrorPage(),needLogin: false),//错误页面错误页面
  PRODUCT_PAGE:AppPage((context)=>const ProductPage()),//产品列表
  // GOODS_INFO_PAGE:AppPage((context)=>const GoodsInfoPage()),//权益商品详情
};

Map<String,Widget Function(BuildContext context)> getRoutes(){
  var map = <String,Widget Function(BuildContext context)>{};
  routes.forEach((key, value) {
    var f = routes[key];
    if(f!=null){
      map[key] =f.page as Widget Function(BuildContext context);
    }
  });
  return map;
}

//固定写法
// ignore: prefer_function_declarations_over_variables
var onGenerateRoute=(RouteSettings settings) {
  // 统一处理
  final String? name = settings.name;
  AppPage? appPage = routes[name];
  if(appPage!=null){
    final Function pageContentBuilder = appPage.page;
    if (pageContentBuilder != null) {
      //登录拦截
      if(appPage.needLogin){
        if (settings.arguments != null) {
          final Route route = MaterialPageRoute(
              builder: (context) =>
                  pageContentBuilder(context, arguments: settings.arguments));
          return route;
        }else{
          final Route route = MaterialPageRoute(
              builder: (context) =>
                  pageContentBuilder(context));
          return route;
        }
      }else{
        //跳转登录
        BuildContext? context = NavKey.navKey.currentState?.overlay?.context;
        if(context!=null){
          NavigatorUtil.goAndFinishAll(context, LOGIN_PAGE);
        }
      }
    }
  }
};